package com.mmall.couplet.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmall.couplet.service.CarService;
import com.mmall.couplet.controller.MyException;
import com.mmall.couplet.model.CarServiceModel;

@Controller
@RequestMapping("/car")
public class CarController extends BaseController {

	@Resource
	private CarService carService;

	/**
	 * 添加车辆服务
	 * 
	 * @param model
	 * @param car_type
	 * @param service_type
	 * @param adderss
	 * @param price
	 * @param status
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	private void insert(HttpServletRequest request, CarServiceModel cModel) {
		try {
			System.out.println("=======添加车辆服务！========");
			cModel.setId(12);
			cModel.setCarType("abcd");
			cModel.setServiceType("2345ab");
			cModel.setStarttime("2018-05-03 15:52:35");
			cModel.setEndtime("2018-05-03 15:42:56");
			cModel.setAdderss("dcc");
			cModel.setPrice("8887dbd");
			cModel.setCreateby("uykk");
			cModel.setCreatedate("2018-05-05 17:02:52");
			cModel.setC3("hz");
			cModel.setC4("sx");
			cModel.setStatus("aaa");
			Boolean carbool = carService.insert(cModel);
			if (carbool == true) {
				System.err.println("======添加车辆服务成功!=======");
			} else {
				System.out.println("======添加车辆服务失败！======");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 刷新车辆服务界面已存在的车辆服务列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/showCarServiceInfo")
	public String showCarServiceInfo(HttpServletRequest request) throws Exception {

		System.out.println("======车辆服务查询======");
		try {
			List<CarServiceModel> carServiceList = carService.queryAllCarServiceModel();
			for (int i = 0; i < carServiceList.size(); i++) {
				CarServiceModel carser = carServiceList.get(i);
				Integer id = carser.getId();
				String address = carser.getAdderss();
				String c3 = carser.getC3();
				String c4 = carser.getC4();
				String cartype = carser.getCarType();
				String createby = carser.getCreateby();
				String createdate = carser.getCreatedate();
				String endtime = carser.getEndtime();
				String price = carser.getPrice();
				System.out.println(address);
				System.out.println(c3);
				System.out.println(c4);
				System.out.println(cartype);
				System.out.println(createby);
				System.out.println(createdate);
				System.out.println(endtime);
				System.out.println(price);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "showserver";
	}

	@RequestMapping("/deleteCarServiceInfo")
	@ResponseBody
	public String deleteCarServiceInfo(HttpServletRequest request, Integer id) {
		try {
			System.out.println("=======车辆服务删除！========");
			Map<String, Object> reMap = new HashMap<String, Object>();
			reMap.put("code", "1");
			Boolean carbool = carService.deleteCarServiceModel(id);
			if(carbool==true){
				System.out.println("======车辆服务删除成功！======");
			}else{
				System.out.println("======车辆服务删除失败！======");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "deleteok";
	}

	/**
	 * 编辑更改信息
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateCarServiceInfo")
	@ResponseBody
	public String updateCarServiceInfo(HttpServletRequest request, Integer id) {
			Map<String, Object> reMap = new HashMap<String, Object>();
			reMap.put("code", "1");
			if (id != null) {
				CarServiceModel carServiceList = carService.updateCarServiceModel(id);
				if (carServiceList != null) {
					reMap.put("carServiceList", carServiceList);
					reMap.put("code", "0");
				}
			}
			return "updateok";
	}

	/**
	 * 服务信息
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/saveCarServiceInfo")
	@ResponseBody
	public Map<String, Object> saveMemberInfo(Model model, @RequestParam(value = "id") Integer id) {

			System.out.println("========编辑车辆服务=======");
			
			CarServiceModel carServiceList = carService.updateCarServiceModel(id);
			Integer cid =carServiceList.getId();
			String ccar_type = carServiceList.getCarType();
			String cservice_type = carServiceList.getServiceType();
			String cstartTime = carServiceList.getStarttime();
			String cendTime = carServiceList.getEndtime();
			String cadderss = carServiceList.getAdderss();
			String cprice = carServiceList.getPrice();
			String ccreateBy = carServiceList.getCreateby();
			String ccreateDate = carServiceList.getCreatedate();
			String cc3 = carServiceList.getC3();
			String cc4 = carServiceList.getC4();
			String cstatus = carServiceList.getStatus();
			
			Map<String, Object> reMap = new HashMap<String, Object>();
			CarServiceModel cModel = new CarServiceModel();
			cModel.setId(cid);
			cModel.setCarType(ccar_type);
			cModel.setServiceType(cservice_type);
			cModel.setStarttime(cstartTime);
			cModel.setEndtime(cendTime);
			cModel.setAdderss(cadderss);
			cModel.setPrice(cprice);
			cModel.setCreateby(ccreateBy);
			cModel.setCreatedate(ccreateDate);
			cModel.setC3(cc3);
			cModel.setC4(cc4);
			cModel.setStatus(cstatus);
			Boolean sabool = carService.saveMemberCarServiceModel(cModel);
			if(sabool==true){
				System.out.println("=======编辑车辆服务成功！======");
			}else{
				System.out.println("=======编辑车辆服务失败!=======");
			}
			reMap.put("code", "0");
			reMap.put("message", "修改信息成功！");
			return reMap;

	}
}
